package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.RoleDO;
import com.bootdo.system.domain.TbOrder;
import com.bootdo.system.domain.TbWxmessage;
import com.bootdo.system.domain.TbWxuser;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.service.MsgService;
import com.bootdo.system.service.OrderService;
import com.bootdo.system.service.RoleService;
import com.bootdo.system.service.WxUserService;

@RequestMapping("/msg")
@Controller
public class MsgController {
	
	@Autowired
	private MsgService msgService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private WxUserService wxUserService;
	
	String prefix = "system/msg";
	
	@GetMapping()
	String product() {
		return prefix + "/msg";
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public PageUtils gzhConfigList(@RequestParam Map<String, Object> params) {
		UserDO user = ShiroUtils.getUser();
		if(user.getUserId() != 1L) {
			List<RoleDO> list2 = roleService.getRoleByUserId(user.getUserId());
			boolean falg=false;
			for (RoleDO role : list2) {
				if(role.getRoleId() == 59L) {
					falg=true;
				}
			}
			if(falg) {
				params.put("deptId", user.getDeptId());
			}else {
				params.put("sid", user.getUserId());
			}
		}
		Query query = new Query(params);
		 List<TbWxmessage> list = msgService.wxMsgList(query);
		 for (TbWxmessage tbWxmessage : list) {
			 String openid = tbWxmessage.getTitle();
			 TbWxuser userById = wxUserService.getUserById2(openid);
			 if(userById != null) {
			 	Integer id = userById.getId();
				tbWxmessage.setWxId(userById.getNickname()+" ( ID : "+id+" ) ");
			 }
		}
		 int count = msgService.count(query);
		 PageUtils pageUtil = new PageUtils(list, count);
		 return pageUtil;
	}
	
	
	@PostMapping("/remove")
	@ResponseBody
	R remove(Integer id) {
		if (msgService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	@PostMapping("/batchRemove")
	@ResponseBody
	R batchRemove(@RequestParam("ids[]") Integer[] ids) {
		int r = msgService.batchremove(ids);
		if (r > 0) {
			return R.ok();
		}
		return R.error();
	}

}
